﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.SalesOrder;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.SalesOrder
{
    public class OrderSaleServices : IOrderSaleServices
    {
        public OrderSaleServices (IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get;}
        public async Task<bool> AddAsync(OrderSale model, List<OrderSaleItem> list)
        {
            var result = await Db.InsertIdentityTranAsync(model, list);
            return result;
        }

        public async Task <bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete OrderSale where Id in @Ids;delete OrderSaleItem where OrderSaleId in @Ids;";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<IEnumerable<OrderSale>> GetAsync(int PageIndex, int PageSize, string Number, string CustomerNumber, string WarehouseId, string Status)
        {
            string sql = "select * from OrderSale where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(CustomerNumber))
            {
                sql += " and CustomerNumber like @CustomerNumber ";
            }

            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await Db.QueryAsync<OrderSale>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", CustomerNumber = $"%{CustomerNumber}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string Number, string CustomerNumber, string WarehouseId, string Status)
        {
            string sql = "select count(*) from OrderSale where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(CustomerNumber))
            {
                sql += " and CustomerNumber like @CustomerNumber ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))  
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (!string.IsNullOrEmpty(Status))
            {
                sql += " and Status like @Status ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", CustomerNumber = $"%{CustomerNumber}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%" });
            return count;
        }

        public async Task<IEnumerable<OrderSale>> GetllAsync()
        {
            string sql = "select * from OrderSale";
            var list = await Db.QueryAllAsync<OrderSale>(sql);
            return list;
        }

        public async Task<IEnumerable<OrderSaleItemDto>> GetOrderSalebyIdAsync(string OrderSaleId, int WarehouseId)
        {
            var list = await Db.QueryAsync<OrderSaleItemDto>(@"
select item.Spec,item.ItemName,item.ItemCode,item.UnitOfMeasure,(ik.StockCount-ik.SalesCount) as CurrCount,oq.* from OrderSaleItem oq
inner join Item item on oq.ItemId = item.Id inner join ItemStock ik on ik.ItemId=item.Id and ik.WareHouseId=@WarehouseId where oq.OrderSaleId = @OrderSaleId", new { OrderSaleId ,WarehouseId });
            return list;
        }

        public async Task<bool> UpdateAsync(OrderSale model, List<OrderSaleItem> list)
        {
            var result = await Db.UpdateIdentityTranAsync(model, list);
            return result;
        }

        public async Task<bool> UpdateOrderStatus(int Id, string Status)
        {
            string sql = $"update OrderSale set Status=@Status where Id=@Id";
            var result = await Db.UpdateAsync(sql, new { Id, Status });
            return result;
        }
    }
}
